#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <signal.h>
#include <setjmp.h>
#include <unistd.h>

#include "login.h"
#include "getch.h"
#include "game_function.h"
#include "show.h"

#define UNUSED(x)  ((void)x)
static jmp_buf buf;

static void deal_ctrl_c(int sig)
{
    longjmp(buf, 1);
}

/*
 *@brief        更改执行位置
 */
static void stringreplace(const char *position)
{
    char *location = (char *)malloc(80*2);
    char *site = NULL;

    getcwd(location, 80);
    strcat(location, position + 1);
    site = strstr(location, "build/bin");
    *site = '\0';

    chdir(location);
    free(location);
}

int main(int argc, char *argv[])
{  
    int ret = 1;
    struct core *core = NULL;

    UNUSED(argc);
    stringreplace(argv[0]);

    signal(SIGINT, deal_ctrl_c);

    core = init();

    ret = setjmp(buf);

    if(ret != 1) 
    run(core);

    destory(core);

    return 0;
}
